<template>
  <div class="comitem icon">
    <el-input
      type="text"
      :placeholder="placeholder"
      minlength="2"
      maxlength="8"
      class="post"
      v-model="fromUser"
      :disabled="disabled"
      v-if="!disabled"
    />
    <el-input
      type="textarea"
      placeholder="请输入内容"
      minlength="2"
      maxlength="150"
      show-word-limit
      resize="none"
      rows="3"
      style="width:100%"
      v-model="content"
      :id="inputID"
    />
    <div class="combottom">
      <div class="one">
        <div class="emoji_box">
          <img
            class="emoji-panel-btn"
            src="/images/face.png"
            @click="changeShow"
          />
          <chat-emoji
            class="emoji"
            :page="type"
            @addEmoji="appendEmoji"
            v-show="showEmoji"
          ></chat-emoji>
        </div>
        <div class="imgbox">
          <img
            v-if="!getToken"
            class="emoji-panel-btn select-img"
            src="/images/avatars.png"
            @click="changeAvatars"
          />
          <span
            v-if="!getToken"
            v-html="imgSrc ? '用户头像:' : '未选择用户头像'"
          ></span>
          <img v-show="imgSrc" class="emoji-panel-btn" :src="imgSrc" alt="" />

          <div class="avatars_box" v-show="showAvatars">
            <div v-for="(item, index) in 8" :key="index">
              <img
                :src="'/images/' + item + '.png'"
                alt=""
                @click="changeIndex(index)"
              />
            </div>
          </div>
        </div>
      </div>
      <div class="two">
        <el-button type="primary" @click="postComment" style="width:70px"
          >评论</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import ChatEmoji from "../chatEmoji/emoji"

import { mapGetters } from "vuex"
export default {
  data() {
    return {
      placeholder: "",
      showEmoji: false,
      content: "",
      fromUser: "",
      disabled: false,
      type: "other",
      imgSrc: "",
      showAvatars: false,
    }
  },
  created() {
    // console.log(this.from)
  },
  computed: {
    ...mapGetters(["getToken", "getUser"]),
  },
  // 需要传入inputID ，在执行focus时需要使用到，避免页面中出现同样的id
  props: ["inputID"],
  mounted() {
    this.disabled = Boolean(this.getToken)
    this.placeholder = this.getToken
      ? "已经登录，不需要输入用户名"
      : "请输入用户名"
  },
  components: {
    ChatEmoji,
  },
  methods: {
    postComment() {
      let fromUser = {}
      if (this.getToken) {
        fromUser = {
          name: this.getUser.userName,
          isAdmin: this.getUser.isAdmin,
          photo: this.getUser.photo,
          account: this.getUser.account,
        }
      } else if (!this.imgSrc) {
        this.$notify.error({
          title: "错误",
          message: "未选择用户头像",
        })
        return
      } else if (this.fromUser == "" || this.content == "") {
        this.$notify.error({
          title: "错误",
          message: "用户名或者评论不能为空",
        })
        return
      } else {
        fromUser = {
          name: this.fromUser,
          isAdmin: false,
          photo: this.imgSrc,
        }
      }

      this.$emit("handleCom", {
        fromUser,
        c_content: this.content,
        c_date: new Date(),
      })
      this.content = ""
      this.fromUser = ""
      this.showEmoji = false
    },
    // 选择头像
    changeIndex(index) {
      this.imgSrc = "/images/" + (index + 1) + ".png"
      this.showAvatars = false
    },
    // 添加表情
    appendEmoji(emoji) {
      this.content = this.content + emoji
      this.showEmoji = false
      let input_dom = document.getElementById(this.inputID)
      input_dom.focus()
    },
    changeShow() {
      this.showEmoji = !this.showEmoji
      this.showAvatars = false
    },
    changeAvatars() {
      this.showAvatars = !this.showAvatars
      this.showEmoji = false
    },
  },
}
</script>
<style lang="scss" scoped>
.comitem {
  margin: 10px 0px;
  width: 100%;
  .combottom {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    .one {
      display: flex;
      position: relative;
      .emoji_box {
        width: 50px;
        height: 50px;
        position: relative;
        .emoji-panel-btn {
          display: inline-block;
          height: 35px;
          width: 35px;
          vertical-align: middle;
          &:hover {
            cursor: pointer;
          }
        }
      }
      .emoji {
        position: absolute;
        top: -190px;
        left: 0px;
      }
      .imgbox {
        display: inline-block;
        vertical-align: middle;
        position: relative;
        img {
          border-radius: 5px;
          width: 38px;
          // margin-right: 5px;
          vertical-align: top;
          // filter: blur(1px);
        }
        .select-img {
          width: 35px;
          height: 35px;
          user-select: none;
        }
        img:hover {
          cursor: pointer;
        }
        span {
          line-height: 40px;
          height: 40px;
          padding-left: 15px;
          padding-right: 10px;
          color: rgb(192, 190, 190);
        }
        .avatars_box {
          position: absolute;
          top: -160px;
          left: 0;
          width: 300px;
          height: 150px;
          padding: 0px;
          box-shadow: 0px 0px 10px 1px rgba(0, 0, 0, 0.3);
          display: flex;
          flex-wrap: wrap;
          background: white;
          border-radius: 5px;
          div {
            width: 25%;
            padding: 10px;
            box-sizing: border-box;
            img {
              width: 100%;
              height: 100%;
              object-fit: cover;
            }
          }
        }
        .active {
          opacity: 1;
        }
      }
    }
    .two {
      text-align: right;
    }
  }
  .post {
    margin: 10px 0px;
  }
}
</style>
